<?php
namespace app\services;

use think\Db;
use think\facade\Config;

class OrderCount
{

    /**
     * 更新所有渠道的月报表
     * @Author   lf
     * @DateTime 2019-10-16T15:06:48+0800
     * @param    [type]                   $data [description]
     * @return   [type]                         [description]
     */
    public function fire($data)
    {

        //查询当月全部日期
        $j = date('t', strtotime($data['seletime'] . "-01"));
        // $j = date("t"); //获取当前月份天数
        $start_time = strtotime(date($data['seletime'] . '-01')); //获取本月第一天时间戳
        $array = array();
        for ($i = 0; $i < $j; $i++) {
            $array[] = date('Y-m-d', $start_time + $i * 86400); //每隔一天赋值给数组
        }
        //查询有订单的日期
        $orderTableArray = [];
        foreach ($array as $key => $value) {

            $ordertime = substr(str_replace("-", "", $value), 2);

            if (Db::query("SHOW TABLES LIKE 'bus_order_{$ordertime}'")) {
                $orderTableArray[] = $value;
            }
        }

        // dump($orderTableArray);
        //获取redis值
        $allData = [];
        $ZD_REDIS_CONFIG = Config::get('ZD_REDIS_CONFIG');
        $redis = redisCurl($ZD_REDIS_CONFIG['IP'], $ZD_REDIS_CONFIG['PORT'], $ZD_REDIS_CONFIG['PASSWORD'], 6);
        // $redis = redisCurl("120.78.70.192", "6379", "fuchen@123", 6);

        //分运营商
        $operators = [
            ['name' => '移动', 'host' => '_10086']
            , ['name' => '联通', 'host' => '_10010']
            , ['name' => '电信', 'host' => '_10000']
            , ['name' => '三网', 'host' => '_90001'],
        ];

        //循环获取每一天的数据
        foreach ($orderTableArray as $key => $value) {

            $cidArray = [session('relate_id')];
            // $cidArray = Db::table('bus_order_'.substr(str_replace("-","",$value), 2))->field('cid')->group('cid')->column('cid');

            foreach ($cidArray as $k => $v) {

                for ($i = 0; $i < count($operators); $i++) {
                    # code...
                    $wheret = str_replace("-", "", $value);

                    $qianz = 'REPORT_C_' . $wheret . '_' . $v . $operators[$i]['host'] . '_';

                    $sms_all = $redis->get($qianz . 'PT');

                    if ($sms_all) {
                        $sms_success = $redis->get($qianz . 'PS');
                        if (!$sms_success) {
                            $sms_success = 0;
                        }
                        $sms_faild = $redis->get($qianz . 'PF');
                        if (!$sms_faild) {
                            $sms_faild = 0;
                        }
                        $amt_sms_success = $redis->get($qianz . 'PM');
                        if (!$amt_sms_success) {
                            $amt_sms_success = 0;
                        }

                        $dataOne = [];
                        $dataOne['sms_all'] = $sms_all;
                        $dataOne['sms_success'] = $sms_success;
                        $dataOne['sms_faild'] = $sms_faild;
                        $dataOne['sms_sending'] = $sms_all - ($sms_success + $sms_faild);
                        $dataOne['amt_sms_success'] = bcdiv($amt_sms_success, 1000, 5);

                        $allData[] = [$value, $dataOne];

                        $dataOne['create_time'] = $value;
                        $dataOne['channel_id'] = $v;

                        if (Db::table('bus_order_report')->where('channel_id', $dataOne['channel_id'])->where('isp_host', $operators[$i]['name'])->where('create_time', $dataOne['create_time'])->find()) {
                            Db::table('bus_order_report')->where('channel_id', $dataOne['channel_id'])->where('isp_host', $operators[$i]['name'])->where('create_time', $dataOne['create_time'])->update($dataOne);
                        } else {
                            $dataOne['isp_host'] = $operators[$i]['name'];
                            Db::table('bus_order_report')->insert($dataOne);
                        }
                    }

                }
            }

            usleep(200);
        }
        return $allData;
        //生成报表
        // dump($allData);
    }

    /**
     * 更新一天所有渠道的报表
     * @Author   lf
     * @DateTime 2019-10-16T15:08:08+0800
     * @param    [type]                   $data [description]
     * @return   [type]                         [description]
     */
    public function fireDate($data)
    {
        //查看当前时间是否在规定时间内
        // $time = time();
        // if($time >= strtotime(date('Y-m-d 00:00:00')) && $time <= strtotime(date('Y-m-d 01:00:00') ))
        // {
        $ordertime = substr($data, 2);

        //查询当天产生订单的渠道号
        if (!Db::query("SHOW TABLES LIKE 'bus_order_{$ordertime}'")) {
            return false;
        }

        $cidArray = Db::table('bus_order_' . $ordertime)->field('cid')->group('cid')->column('cid');

        //获取redis值
        //查询ridis
        $ZD_REDIS_CONFIG = Config::get('ZD_REDIS_CONFIG');
        $redis = redisCurl($ZD_REDIS_CONFIG['IP'], $ZD_REDIS_CONFIG['PORT'], $ZD_REDIS_CONFIG['PASSWORD'], 6);
        // $redis = redisCurl("120.78.70.192", "6379", "fuchen@123", 6);

        // $today = date('Y-m-d');
        foreach ($cidArray as $k => $v) {
            if (empty($v)) {
                continue;
            }
            //拼接前缀
            // $wheret = str_replace("-","",$data);
            $qianz = 'REPORT_C_' . $data . '_' . $v . '_';
            //获取全部短信
            $sms_all = $redis->get($qianz . 'PT');

            if ($sms_all) {
                $sms_success = $redis->get($qianz . 'PS');
                if (!$sms_success) {
                    $sms_success = 0;
                }
                $sms_faild = $redis->get($qianz . 'PF');
                if (!$sms_faild) {
                    $sms_faild = 0;
                }
                $amt_sms_success = $redis->get($qianz . 'PM');
                if (!$amt_sms_success) {
                    $amt_sms_success = 0;
                }

                $dataOne = [];
                $dataOne['sms_all'] = $sms_all;
                $dataOne['sms_success'] = $sms_success;
                $dataOne['sms_faild'] = $sms_faild;
                $dataOne['sms_sending'] = $sms_all - ($sms_success + $sms_faild);
                $dataOne['amt_sms_success'] = bcdiv($amt_sms_success, 1000, 5);
                $dataOne['create_time'] = $data;
                $dataOne['channel_id'] = $v;

                // 更新表
                if (Db::table('bus_order_report')->where('channel_id', $dataOne['channel_id'])->where('create_time', $dataOne['create_time'])->find()) {
                    Db::table('bus_order_report')->where('channel_id', $dataOne['channel_id'])->where('create_time', $dataOne['create_time'])->update($dataOne);
                } else {
                    Db::table('bus_order_report')->insert($dataOne);
                }
            }

        }

        return true;
        // }else{
        //     //校验时间
        //     return false;
        // }
    }
    /**
     * 更新一天所有通道的报表
     * @Author   lf
     * @DateTime 2019-10-16T15:08:08+0800
     * @param    [type]                   $data [description]
     * @return   [type]                         [description]
     */
    public function firePlatDate($data)
    {

        // 拼接日期
        $ordertime = substr($data, 2);

        //查询当天产生订单的渠道号
        if (!Db::query("SHOW TABLES LIKE 'bus_add_order_{$ordertime}'")) {
            return false;
        }

        //当天发送的通道
        $platArray = Db::table('bus_add_order_' . $ordertime)->field('plat_id')->group('plat_id')->column('plat_id');

        //获取redis值
        //查询ridis
        $ZD_REDIS_CONFIG = Config::get('ZD_REDIS_CONFIG');
        $redis = redisCurl($ZD_REDIS_CONFIG['IP'], $ZD_REDIS_CONFIG['PORT'], $ZD_REDIS_CONFIG['PASSWORD'], 6);
        // $redis = redisCurl("120.78.70.192", "6379", "fuchen@123", 6);
        //分运营商
        $operators = [
            ['name' => '移动', 'host' => '_10086']
            , ['name' => '联通', 'host' => '_10010']
            , ['name' => '电信', 'host' => '_10000']
            , ['name' => '三网', 'host' => '_90001'],
        ];

        //循环获取
        foreach ($platArray as $k => $v) {
            if (empty($v)) {
                continue;
            }
            for ($i = 0; $i < count($operators); $i++) {
                //拼接前缀
                // $wheret = str_replace("-","",$data);
                $qianz = 'REPORT_P_' . $data . '_' . $v . $operators[$i]['host'] . '_';
                # code...
                //获取全部短信
                $sms_all = $redis->get($qianz . 'PT');

                if ($sms_all) {
                    $sms_success = $redis->get($qianz . 'PS');
                    if (!$sms_success) {
                        $sms_success = 0;
                    }
                    $sms_faild = $redis->get($qianz . 'PF');
                    if (!$sms_faild) {
                        $sms_faild = 0;
                    }
                    $amt_sms_success = $redis->get($qianz . 'PM');
                    if (!$amt_sms_success) {
                        $amt_sms_success = 0;
                    }

                    $dataOne = [];
                    $dataOne['sms_all'] = $sms_all;
                    $dataOne['sms_success'] = $sms_success;
                    $dataOne['sms_faild'] = $sms_faild;
                    $dataOne['sms_sending'] = $sms_all - ($sms_success + $sms_faild);
                    $dataOne['amt_sms_success'] = bcdiv($amt_sms_success, 1000, 5);
                    $dataOne['create_time'] = $data;
                    $dataOne['plat_id'] = $v;

                    // 更新表
                    if (Db::table('bus_order_plat_report')->where('plat_id', $dataOne['plat_id'])->where('isp_host', $operators[$i]['name'])->where('create_time', $dataOne['create_time'])->find()) {
                        Db::table('bus_order_plat_report')->where('plat_id', $dataOne['plat_id'])->where('isp_host', $operators[$i]['name'])->where('create_time', $dataOne['create_time'])->update($dataOne);
                    } else {
                        $dataOne['isp_host'] = $operators[$i]['name'];
                        Db::table('bus_order_plat_report')->insert($dataOne);
                    }
                }
            }

        }

        return true;

    }
}
